﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Sharp.Infrastructure;
using Sharp.Portal.Configuration;
using Sharp.Portal.Mvc;
// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    /// <inheritdoc />
    /// <summary>
    /// 系统设置
    /// </summary>
    [Authorize]
    public class SettingController : SharpController
    {
        [AutomaticTemplate]
        public ActionResult Index()
        {
            var model = SystemConfigManage.Read<PlatformInfo>();
            return View(model);
        }

        
        public ActionResult Save(PlatformInfo vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result { Err = "提交数据出错。" });
            }

            HttpPostedFileBase uploadFile = Request.Files["LogoUrlFileUpload"];

            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                var stream = uploadFile.InputStream;

                byte[] inputBytes = new byte[stream.Length];
                stream.Read(inputBytes, 0, inputBytes.Length);
                stream.Seek(0, SeekOrigin.Begin);

                vm.LogoUrl = $"data:{uploadFile.ContentType};base64," + Convert.ToBase64String(inputBytes);
            }


            SystemConfigManage.Save(vm);
            HttpRuntime.Close();
            return this.JsonNet(new Result()
            {
                Message = "保存成功。"
            });
        }

    }
}